package com.web.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.web.entity.User;
import com.web.entity.UserInfo;
import com.web.provider.UserProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    User Sel(Integer id);

    @Select("select * from user where id = #{id}")
    User Sel2(Integer id);


    List<UserInfo> selectUserInfoList();


    /**
     * 自定义方法，展示所有用户的用户名
     * @return 用户名集合
     */
    @Select("select name from user")
    List<String> listAllUsername();

    /**
     * 自定义方法，搜索所有用户的邮箱，并封装进 user 对象里
     */
    @Select("select email from user")
    @Results(
            @Result(column = "email", property = "email")
    )
    List<User> listAllEmail();



    /**

     * 获取用户信息和身份证信息

     * 一对一关联查询

     */

    @Select("SELECT * FROM user WHERE id = #{id}")
    @Results(id = "userAndInfoResultMap", value = {
//            @Result(property = "id", column = "id", javaType = Integer.class, jdbcType = JdbcType.INTEGER, id = true),
            @Result(property = "name", column = "name"),
            @Result(property = "age", column = "age"),
            @Result(property = "userInfo",column = "id",
                    one = @One(select = "com.web.mapper.UserMapper.getUserInfo", fetchType = FetchType.LAZY))
    })

    User getUserAndInfo(@Param("id")int id);


    @Select("SELECT * FROM user_info WHERE user_id = #{id}")
    @Results(id = "userInfoResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "address", column = "address")})
    UserInfo getUserInfo(@Param("id")int id);



    @SelectProvider(type= UserProvider.class,method="queryUsersByParamsSql2")
    List<User> queryUsersByParams(
            @Param("name") String name
    );

}
